<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      @click-left="$router.back()"
      left-arrow
      title="登录"
    />
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" :show-error="false">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        type="number"
        :rules="rules.mobile"
        clearable
      >
        <!--【增加图标】-->
        <i slot="left-icon" class="toutiao toutiao-wode2"></i>
      </van-field>
      <van-field
        v-model="user.password"
        name="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
        :rules="rules.password"
        clearable
      >
        <!--【增加图标】-->
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          round
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <!-- /登录表单 -->
    <div class="buttom_text">
      <span class="lt" @click="$router.push('/sign')">前往注册</span>
      <span class="rt" @click="$router.push('/reset')">忘记密码</span>
    </div>
  </div>
</template>

<script>
import { loginUser } from "@/api/cuixingkui/user.js";
export default {
  name: "Login",
  data() {
    return {
      user: {
        mobile: "13800000005",
        password: "222222a",
      },
      rules: {
        mobile: [
          { required: true, trigger: blur, message: "请输入手机号" },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "手机号格式错误",
          },
        ],
        password: [
          { required: true, trigger: blur, message: "请输入密码" },
          // {
          //   pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
          //   message: "密码至少包含 数字和英文，长度6-20",
          // },
        ],
      },
    };
  },
  created() {},
  methods: {
    async onSubmit() {
    
      //获取表单数据
      const user = this.user;
      //校验表单数据
      //发起登录请求
      let {data}= await loginUser(user);
      // console.log(data.data.token);
        console.log(data.data);
      if (data.errno === 1000) {
      
        return this.$toast.fail(data.errmsg);
      } else {
        console.log(222);
        //成功和失败的处理
        this.$toast.success("登录成功！");
        //存入token
        this.$store.commit("setUser", data.data.token);
        this.$store.commit("setUserInfo", data.data.userInfo);
        // this.$store.commit("setUserInfo", data.data.userinfo);
        this.$router.push("/user");
        // console.log(data);
      }
    },
  },
};
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #ee0a24;
      border: none;
    }
  }
  .buttom_text {
    color: red;
    font-size: 30px;
    .rt {
      float: right;
      margin-right: 20px;
    }
    .lt {
      float: left;
      margin-left: 20px;
    }
  }
}
</style>
